/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.interaction3d.assembly.convert.collada;

import org.interaction3d.assembly.convert.*;

/**
 *
 * @author micha
 */
public final class Triangle
{
    public final int a, b, c;

    public Triangle(int a, int b, int c)
    {
        //if (a < 0 || b < 0 || c < 0)
        //    throw new IllegalArgumentException(a + " " + b + " " + c);

        this.a = a;
        this.b = b;
        this.c = c;
    }

    static public void remap(Triangle[] triangles, int[] map)
    {
        for(int i=0; i<triangles.length; i++)
        {
            Triangle tri = triangles[i];
            triangles[i] = new Triangle(map[tri.a], map[tri.b], map[tri.c]);
        }
    }

    static public void remap(int[][] triangles, int[] map)
    {
        for(int i=0; i<triangles.length; i++)
        {
            int[] tri = triangles[i];
            triangles[i][0] = map[tri[0]];
            triangles[i][1] = map[tri[1]];
            triangles[i][2] = map[tri[2]];
        }
    }


    static public Triangle[] smooth(Smooth smooth, Triangle[] positions, Triangle[] normals)
    {
        if(positions.length != normals.length) throw new IllegalArgumentException();

        int numTriangles = positions.length;

        Triangle[] triangles = new Triangle[numTriangles];
        for(int i=0; i<numTriangles; i++)
        {
            int a = smooth.apply(positions[i].a, normals[i].a);
            int b = smooth.apply(positions[i].b, normals[i].b);
            int c = smooth.apply(positions[i].c, normals[i].c);
            triangles[i] = new Triangle(a, b, c);
        }
        return triangles;
    }

    static public void convert(Vertices vertices, Triangle[][] src, Triangle[] dst, int offset)
    {
        int numTriangles = src[0].length;
        int numAttributes = src.length;

        System.out.println("NumTris: " + numTriangles);
        System.out.println("NumAttr: " + numAttributes);

        for (int tri = 0; tri < numTriangles; tri++)
        {
            int[] aIndex = new int[numAttributes];
            int[] bIndex = new int[numAttributes];
            int[] cIndex = new int[numAttributes];

            for (int attr = 0; attr < numAttributes; attr++)
            {
                aIndex[attr] = src[attr][tri].a;
                bIndex[attr] = src[attr][tri].b;
                cIndex[attr] = src[attr][tri].c;
            }

            dst[offset + tri] = new Triangle(vertices.apply(aIndex), vertices.apply(bIndex), vertices.apply(cIndex));
        }
    }

    static public void convert(Vertices vertices, Triangle[][] src, int[][] dst, int offset)
    {
        int numTriangles = src[0].length;
        int numAttributes = src.length;

        System.out.println("NumTris: " + numTriangles);
        System.out.println("NumAttr: " + numAttributes);

        for (int tri = 0; tri < numTriangles; tri++)
        {
            int[] aIndex = new int[numAttributes];
            int[] bIndex = new int[numAttributes];
            int[] cIndex = new int[numAttributes];

            for (int attr = 0; attr < numAttributes; attr++)
            {
                aIndex[attr] = src[attr][tri].a;
                bIndex[attr] = src[attr][tri].b;
                cIndex[attr] = src[attr][tri].c;
            }

            dst[offset + tri] = new int[] { vertices.apply(aIndex), vertices.apply(bIndex), vertices.apply(cIndex) };
        }
    }

}